//
//  ViewController2.m
//  SaveDate
//
//  Created by 胡先剑 on 16/3/15.
//  Copyright © 2016年 胡先剑. All rights reserved.
//

#import "ViewController2.h"

@interface ViewController2 ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) NSMutableArray *stuList;
@property (weak, nonatomic) IBOutlet UITextField *idTextField;
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *ageTextField;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

@implementation ViewController2

- (NSMutableArray *)stuList
{
    if (_stuList == nil)
    {
        NSFileManager *fm = [NSFileManager defaultManager];
        if ([fm fileExistsAtPath:[self plistPath]])
        {
            _stuList = [NSMutableArray arrayWithContentsOfFile:[self plistPath]];
        }
        else
        {
            _stuList = [NSMutableArray array];
        }
    }
    return _stuList;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
/** plist文件储存位置*/
- (NSString *)plistPath
{
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"students.plist"];
    return filePath;
}

/** 保存数据到plist(覆盖以前的数据)*/
- (void)savePlist
{
    [self.stuList writeToFile:[self plistPath] atomically:YES];
}
- (IBAction)add2:(id)sender
{
    NSMutableDictionary *dict = [@{@"stuId":self.idTextField.text,
                                  @"stuName":self.nameTextField.text,
                                  @"stuAge":self.ageTextField.text} mutableCopy];
    
    [self.stuList addObject:dict];
    
    [self savePlist];
    [self.tableView reloadData];
    
}
- (IBAction)remove2:(id)sender
{
    for (NSMutableDictionary *dict in self.stuList)
    {
        if ([dict[@"stuId"] isEqualToString:self.idTextField.text])
        {
            [self.stuList removeObject:dict];
            break;
        }
    }
    
    [self savePlist];
    [self.tableView reloadData];
}
- (IBAction)modify2:(id)sender
{
    for (NSMutableDictionary *dict in self.stuList)
    {
        if ([dict[@"stuId"] isEqualToString:self.idTextField.text])
        {
            dict[@"stuName"] = self.nameTextField.text;
            dict[@"stuAge"] = self.ageTextField.text;
            break;
        }
    }
    [self savePlist];
    [self.tableView reloadData];
}
- (IBAction)find2:(id)sender
{
    for (NSMutableDictionary *dict in self.stuList)
    {
        if ([dict[@"stuId"] isEqualToString:self.idTextField.text])
        {
            self.nameTextField.text = dict[@"stuName"];
            self.ageTextField.text = dict[@"stuAge"];
            break;
        }
    }
    [self savePlist];
    [self.tableView reloadData];    
}
- (IBAction)findAll:(id)sender
{
    for (NSMutableDictionary *dict in self.stuList)
    {
        NSLog(@"%@ %@ %@",dict[@"stuId"],dict[@"stuName"],dict[@"stuAge"]);
    }
}

#pragma mark - 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.stuList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    UILabel *labrl1 = [cell.contentView viewWithTag:101];
    UILabel *labrl2 = [cell.contentView viewWithTag:102];
    UILabel *labrl3 = [cell.contentView viewWithTag:103];
    
    NSDictionary *dict = self.stuList[indexPath.row];
    labrl1.text = dict[@"stuId"];
    labrl2.text = dict[@"stuName"];
    labrl3.text = dict[@"stuAge"];
    return cell;
}

@end
